package com.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javabean.Permission;
import com.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/permission")
public class PermissiomController {

    @Autowired
    PermissionService permissionService;

    @RequestMapping("/findAll")
    public String findAll(HttpServletRequest request, @RequestParam(value = "page",defaultValue = "1") int page, @RequestParam(value = "limit",defaultValue = "5") int limit){
        PageHelper.startPage(page,limit);
        List<Permission> all = permissionService.findAll();
        PageInfo<Permission> pageInfo=new PageInfo<>(all);
        request.setAttribute("pageInfo",pageInfo);
        return "/permission/permission-list";
    }

    @RequestMapping("/add")
    public String add(Permission permission){
        permissionService.add(permission);
        return "redirect:findAll";
    }

    @RequestMapping("/deleteById")
    public String deleteById(int id){
        permissionService.deleteById(id);
        return "redirect:findAll";
    }

    @RequestMapping( "/findById")
    public String findById(HttpServletRequest request,int id){
        Permission permission = permissionService.findById(id);
        request.setAttribute("permission",permission);
        return "/permission/permission-edit";
    }

    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public String edit(Permission permission){
        permissionService.edit(permission);
        return "redirect:findAll";
    }
}
